import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'network_manager.dart';
import 'constants.dart';
import 'userModelSingleton.dart';
import 'my_card_model.dart';
import 'package:event_bus/event_bus.dart';
import 'custom_notification.dart';

class MyCardsPage extends StatefulWidget {
  @override
  _MyCardsPageState createState() => _MyCardsPageState();
}
class _MyCardsPageState extends State<MyCardsPage> {
  List<MyCard> _cardList = [];
  @override
  void initState() {
    super.initState();
    _fetchCardList();
  }
  Future<void> _fetchCardList() async {
    final String? userPhone = UserModelSingleton().userModel?.userphone;
    if (userPhone == null) {
      Fluttertoast.showToast(
        msg: "没有手机号参数",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
      );
      return;
    }
    final String mchid = userPhone; // 替换为实际的商户ID
    var param = {'userPhone': userPhone, 'mchid': mchid};
    NetworkManager().get(
      '$baseUrl$get_card_List_url',
      param,
          (data) {
        print('请求成功: $data');
        setState(() {
          _cardList = List<MyCard>.from(data['data'].map((item) => MyCard.fromJson(item)));
        });
      },
          (error) {
        Fluttertoast.showToast(
          msg: "请求失败: $error",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
        );
      },
    );
  }
  void _addNumber() {
    print('添加号码按钮被点击');
  }
  void _deleteNumber() {
    print('删除按钮被点击');
  }
  void _selectText(String text) {

    eventBus.fire(TextSelectedEvent(text)); // 发送事件
    Navigator.pop(context); // 返回上一页
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(child: Text('我的卡片')),
      ),
      body: Container(
        color: Colors.white,
        child: Column(
          children: [
            Expanded(
              child: RefreshIndicator(
                onRefresh: _fetchCardList,
                child: ListView.builder(
                  itemCount: _cardList.length,
                  itemBuilder: (context, index) {
                    return Column(
                      children: [
                        Padding(
                          padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 12.0),
                          child: Row(
                            children: [
                              Image.asset(
                                'images/add_card_icon.png',
                                width: 24,
                                height: 24,
                              ),
                              SizedBox(width: 7),
                              GestureDetector(
                                onTap: () => _selectText(_cardList[index].iccidMark),
                                child: Text(_cardList[index].iccidMark),
                              ),
                              Spacer(),
                              GestureDetector(
                                onTap: _deleteNumber,
                                child: Container(
                                  width: 20,
                                  height: 20,
                                  child: Icon(
                                    Icons.delete,
                                    size: 20,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Divider(), // 分割线
                      ],
                    );
                  },
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(12, 0, 12, 15),
              child: SizedBox(
                width: double.infinity,
                height: 50,
                child: ElevatedButton(
                  onPressed: _addNumber,
                  child: Text(
                    '添加号码',
                    style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}